<?php

namespace App\Http\Middleware;

use Closure;
use Jenson\BaseAdmin\Models\Admin;
use Illuminate\Http\Request;

class AdminLoginAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        # 通过session来检测用户是否登录
        if ($request->session()->has('is_login')) {
            $admin = Admin::query()->find(session('uid'));
            $user_data = [
                'id'=>$admin['id'],
                'name'=>$admin['name'],
                'username'=>$admin['username'],
                'avatar'=>$admin['avatar'],
                'phone'=>$admin['phone'],
                'activation_code'=>$admin['activation_code'],
                'is_activation'=>$admin['is_activation'],
                'status'=>$admin['status'],
                'roles'=>$admin['roles'],
            ];
            $mid_params = [
                'admin'=>$user_data,
                'admin_id'=>$user_data['id'],
//                'admin_role'=>$admin['roles'],
            ];
            # 添加参数 [数组]
            $request->attributes->add($mid_params);
            # 进入下一层请求
            return $next($request);
        } else {
            return redirect()->route('admin.login');
        }
    }
}
